package com.wisdytech.linkdcs.factory.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.factory.model.Shop;
import com.wisdytech.linkdcs.factory.service.TmShopService;
import com.wisdytech.linkdcs.factory.vo.ShopVO;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * 
 * <p>
 * ClassName: ShopAction
 * </p>
 * <p>
 * Description: 车间管理页面的action层
 * </p>
 * <p>
 * Author: chenbin
 * </p>
 * <p>
 * Date: 2018-5-4
 * </p>
 */
@Controller
@RequestMapping("/factory/shop")
public class ShopAction {
    /**
     * 注入车间tmShopService
     */
    @Autowired
    @Qualifier("tmShopService")
    private TmShopService tmShopService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "factoryModel/workshopManage";
    }
    
    /**
     * 分页条件查询车间管理列表信息
     * @param factoryCode 工序编码
     * @param factoryName 工序名称
     * @param filter 分页参数
     * @return 厂区管理列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listShopByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<ShopVO> listShopByFilter(String shopCode, String shopName, Filter filter) throws IOException{
    	PageInfo<Shop> shops =this.tmShopService.listShopByFilter(shopCode, shopName, filter);
        //转换为前台结果集
        List<Shop> shopList=shops.getList();
        List<ShopVO> shopVOList=new ArrayList<ShopVO>();
        for(Shop shop:shopList){
        	shopVOList.add(DataTransformUtil.changeShopVO(shop));
        }
        return new StandardTableResult<ShopVO>(shops.getTotal(),shopVOList);
    }
    
    /**
     * <p>Description: 保存车间对象</p> 
     * @param model 车间对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveShop/save.mvc")
    @ResponseBody
    public StandardResult saveShop(Shop shop, HttpSession session,HttpServletRequest request) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
        if (StringUtils.isBlank(shop.getPkId())) {
            tmShopService.addShop(shop, loginName);
        }else {
            tmShopService.updateShop(shop, loginName);
        }
        return StandardResult.getSuccessResult();
    }
    
    /**
     * 删除车间信息
     * @param pkId 设备标识id
     * @return 结果对象
     */
    @RequestMapping(value = "/delShopById/del.mvc")
    @ResponseBody
    public StandardResult delShopById(String pkId) {
    	 tmShopService.delShopById(pkId);
        return StandardResult.getSuccessResult();
    }
    /**
     * <p>Description:查询产车间信息列表</p> 
     * @return 车间信息列表集合
     * @throws IOException 
     */
    @RequestMapping(value = "/getShopById/get.mvc")
    @ResponseBody
    public Shop getShopById(String pkId){
        return tmShopService.getShopById(pkId);
    }

    /**
     *
     * @param factoryId 厂区id
     * @return StandardResult
     */
    @RequestMapping(value = "/listAllShopsByFactoryId/get.mvc")
    @ResponseBody
    public StandardResult listAllShopsByFactoryId(String factoryId) {
        List<Shop> list = this.tmShopService.listAllShops(factoryId);
        return StandardResult.getSuccessResult(list);
    }
}
